package cn.pangza.controller;

import cn.pangza.common.mybatisplus.entity.PageRequest;
import cn.pangza.common.mybatisplus.entity.PageResult;
import cn.pangza.common.utils.ResponseData;
import cn.pangza.common.utils.ValidateUtil;
import cn.pangza.user.entity.dto.RoleMenuDto;
import cn.pangza.user.entity.po.Role;
import cn.pangza.user.service.RoleService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:role:list')")
    @PostMapping("/list")
    public ResponseData<List<Role>> getRolePage(@RequestBody Role role) {
        List<Role> pageResult = roleService.getRoleList(role);
       return ResponseData.success(pageResult);
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:role:create')")
    @PostMapping
    public ResponseData createRole(@Validated @RequestBody Role role, BindingResult bindingResult){
        ResponseData responseData = ValidateUtil.doValidate(bindingResult);
        if(ObjectUtils.isNotEmpty(responseData)) return responseData;
        if(!role.getName().startsWith("ROLE_")) {
            role.setName("ROLE_" + role.getName());
        }
        roleService.save(role);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:role:update')")
    @PutMapping
    public ResponseData updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:role:delete')")
    @DeleteMapping("/{id}")
    public ResponseData deleteRole(@PathVariable Integer id) {
        roleService.removeById(id);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:role:read')")
    @GetMapping("/{id}")
    public ResponseData<Role> getRole(@PathVariable Integer id) {
        Role byId = roleService.getById(id);
        return ResponseData.success(byId);
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN')")
    @GetMapping("/role_menu/{id}")
    public ResponseData<List<Integer>> getMenuIdsByRoleId(@PathVariable Integer id) {
        List<Integer> menuIds = roleService.getMenuIdsByRoleId(id);
        return ResponseData.success(menuIds);
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN')")
    @PostMapping("/role_menu")
    public ResponseData saveRoleMenu(@RequestBody RoleMenuDto roleMenuDto) {
        roleService.saveRoleMenu(roleMenuDto);
        return ResponseData.success();
    }

}
